package com.example.mpandroidchart_all.FragmentChart;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.mpandroidchart_all.R;
import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.RadarData;
import com.github.mikephil.charting.data.RadarDataSet;
import com.github.mikephil.charting.data.RadarEntry;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IRadarDataSet;

import java.util.ArrayList;
import java.util.List;

import javax.security.auth.login.LoginException;

public class FragmentRadarChart extends Fragment {
    private RadarChart radarChart;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_radarchart,container,false);
        radarChart=view.findViewById(R.id.radar_chart);

        setXAxis();
        setYAxis();
        initData();
        return view;
    }
    private IRadarDataSet creatingData(String lable) {
        List<RadarEntry> yVals = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            yVals.add(new RadarEntry((float) (Math.random() * 100))); // 生成1-100的随机数
        }
        RadarDataSet ds = new RadarDataSet(yVals, lable);
        ds.setDrawValues(false);
        ds.setDrawFilled(true);
        ds.setFillColor(Color.RED);
        return ds;

    }
    private void setXAxis(){
        XAxis xAxis = radarChart.getXAxis();
        final String [] strings={"Part A","Part B","Part C","Part D","Part E","Part F","Part G","Part H","Part I"};
        xAxis.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                return strings[(int) (value%9)];
            }
        });
    }
    private void setYAxis(){
        YAxis yAxis = radarChart.getYAxis();
        yAxis.setAxisMaximum(150f);
        yAxis.setAxisMinimum(0f);
        yAxis.setLabelCount(6);
        yAxis.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                return (int)value+" $";
            }
        });
    }
    private void initData() {
        radarChart.setRotationEnabled(false);
        radarChart.setExtraOffsets(0f,30f,0f,30f);
        setChartData();
    }
    private void setChartData(){
        List<IRadarDataSet> dataSets = new ArrayList<>();
        dataSets.add(creatingData("图1"));
        dataSets.add(creatingData("图2"));
        RadarData data = new RadarData(dataSets);
        radarChart.setData(data);
        radarChart.invalidate();
    }
}
